package com.example.userbehaviorprediction;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class frag1 extends Fragment {

    private ArrayList<AppInfo> appList = new ArrayList<AppInfo>();



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        List<PackageInfo> packages = getContext().getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            // 如果不是系统应用，则添加至appList
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                //Log.e("psn:",packageInfo.packageName+i);
                AppInfo mAppInfo = new AppInfo();
                mAppInfo.setAppName(packageInfo.applicationInfo.loadLabel(getContext().getPackageManager()).toString());
                // Log.e("name:",mAppInfo.getAppName());
                mAppInfo.setPackageName(packageInfo.packageName);
                mAppInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(getContext().getPackageManager()));
                appList.add(mAppInfo);
            }
        }

        return inflater.inflate(R.layout.frag1,container,false);
    }

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ImageView img=(ImageView) getActivity().findViewById(R.id.imageView2);
        TextView lblTitle=(TextView) getActivity().findViewById(R.id.resapp);
        String name = "无app数据!";
        String sql = "select * from appUse order by times desc limit 1";
        SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(getActivity());
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        if (readableDatabase.isOpen()) {
            Cursor cursor = readableDatabase.rawQuery(sql, null);
            if(cursor.moveToNext()) {
                name = cursor.getString(1);
            }
            cursor.close();
            readableDatabase.close();
        }
        //boolean flag = false;
        int i;
        for (i = 0; i < appList.size(); i++) {
            AppInfo mAppInfo=appList.get(i);
            //Log.e("name:",mAppInfo.getAppName()+appList.size());
            if(mAppInfo.getPackageName().equals(name)){
                //Log.e("name=:",mAppInfo.getAppName());
                //mAppList.add(mAppInfo);
                //name=mAppInfo.getAppName();
                //flag=true;
                break;
                //i=appList.size();
                //icon = mAppInfo.getAppIcon();
            }
        }
        //Log.e("ps:","flag: "+flag);


        AppInfo mAppInfo=appList.get(i);
        name = mAppInfo.getAppName();
        Drawable icon = mAppInfo.getAppIcon();
        img.setImageDrawable(icon);



        lblTitle.setText(name);
    }
}
